home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlb20 / lib / gethostn.c < prev    next >
C/C++ Source or Header  |  1991-11-04  |  880b  |  47 lines

  1. /* gethostname -- for now, fake by looking in environment */
  2. /* (written by Eric R. Smith, placed in the public domain) */
  3.  
  4. #include <stddef.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <fcntl.h>
  8. #include <unistd.h>
  9.  
  10. #define MAXLEN 127
  11.  
  12. int
  13. gethostname(buf, len)
  14.     char *buf;
  15.     size_t len;
  16. {
  17.     char *foo;
  18.     char xbuf[MAXLEN+1];
  19.     int fd, r;
  20.  
  21.     foo = getenv("HOSTNAME");
  22.     if (!foo) {
  23. /* try looking for the file /local/hostname; if it's present,
  24.  * it contains the name, otherwise we punt
  25.  */
  26.         fd = open("/local/hostname", O_RDONLY);
  27.         if (fd >= 0) {
  28.             r = read(fd, xbuf, MAXLEN);
  29.             if (r > 0) {
  30.                 xbuf[r] = 0;
  31.                 foo = xbuf;
  32.                 while (*foo) {
  33.                     if (*foo == '\r' || *foo == '\n')
  34.                         *foo = 0;
  35.                     foo++;
  36.                 }
  37.                 foo = xbuf;
  38.             }
  39.             close(fd);
  40.         }
  41.     }
  42.     if (!foo)
  43.         foo = "unknown";
  44.     strncpy(buf, foo, len);
  45.     return 0;
  46. }
  47.